import api from './api'
import i18n from '../locale/index.js'

class Common {
	constructor() {
		this.i18n = i18n
	}
	/**
	 * 入库 优先匹配明细id，然后再去搜索库存码
	 * @param {Object} code
	 */
	async scanInboundCode(code) {
		let ret = {
			code: 0,
			message: this.i18n.t('scanfun.success'),
			data: null
		}
		let checkId = await api.getInboundDetail({ id: code })
		console.log(checkId, '------checkId')
		if (checkId.code === 0 && checkId.data) {
			ret.data = checkId.data
		} else {
			let checkCode = await api.getInboundDetailByCode({ stockCode: code })
			if (checkCode.code === 0 && checkCode.data) {
				ret.data = checkCode.data
			} else {
				ret.code = 1
				ret.message = this.i18n.t('scanfun.fail')
			}
		}
		return ret
	}
	/**
	 * 出库 优先匹配明细id，然后再去搜索库存码
	 * @param {Object} code
	 */
	async scanOutboundCode(code) {
		let ret = {
			code: 0,
			message: this.i18n.t('scanfun.success'),
			data: null
		}
		let checkId = await api.getOutboundDetail({ id: code })
		console.log(checkId, '------checkId')
		if (checkId.code === 0 && checkId.data) {
			ret.data = checkId.data
		} else {
			let checkCode = await api.getOutboundDetailByCode({ stockCode: code })
			if (checkCode.code === 0 && checkCode.data) {
				ret.data = checkCode.data
			} else {
				ret.code = 1
				ret.message = this.i18n.t('scanfun.fail')
			}
		}
		return ret
	}
	formatDate(value) {
		const data = new Date(value);
		const month = String(data.getMonth() + 1).padStart(2, '0');
		const day = String(data.getDate()).padStart(2, '0');
		const year = data.getFullYear();
		const hours = String(data.getHours()).padStart(2, '0');
		const minutes = String(data.getMinutes()).padStart(2, '0');
		const seconds = String(data.getSeconds()).padStart(2, '0');
		const formattedTime = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
		return formattedTime;
	}

}
export default new Common()